<template>
  <div class="app-container">
    <div class="filter-container" align="center">
      <el-form :inline="true" :model="pageInfo" class="demo-form-inline">

        <el-form-item label="书名">
          <el-input v-model="pageInfo.bookName" placeholder="书名" />
        </el-form-item>

        <el-form-item label="作者">
          <el-input v-model="pageInfo.author" placeholder="作者" />
        </el-form-item>


        <el-form-item label="类型">
          <el-select v-model="category" placeholder="类型">
            <el-option
              v-for="item in options"
              :key="item.category"
              :label="item.category"
              :value="item.category">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item>
          <el-button type="primary" class="filter-item" icon="el-icon-search" @click="onSubmit">查询订单</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table :data="book" border style="width: 100%" stripe>
      <el-table-column
        type="index"
        label="序号"
        align="center"
        width="50">
      </el-table-column>
      <el-table-column label="图片" align="center" width="120">
        <template slot-scope="scope">
          <img :src="'/api'+scope.row.path" width="60px" height="60px">
        </template>
      </el-table-column>
      <el-table-column
        prop="bookName"
        label="书名"
        width="140"
        align="center"
      />
      <el-table-column
        prop="author"
        label="作者"
        width="140"
        align="center"
      />
      <el-table-column
        prop="category"
        label="类别"
        width="100"
        align="center"
      />
      <el-table-column label="会员" align="center" :formatter="sta" width="100">
      </el-table-column>
      <el-table-column
        prop="date"
        label="加入时间"
        width="130"
        align="center"
      />
      <el-table-column
        prop="state"
        label="支付状态"
        width="100"
        align="center"
      />
      <el-table-column
        prop="money"
        label="价格"
        width="60"
        align="center"
        money
      />

      <el-table-column label="操作" align="center">
        <!--                          获取每一次循环时 的元素节点 (v-for) user in users 中的 user    -->
        <template slot-scope="scope">
<!--          -->
          <el-button
            size="mini"
            type="success"
            icon="el-icon-check" circle
            @click="handleAdd(scope.$index, scope.row)"
          >
          </el-button>
<!--          删除-->
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete" circle
            @click="orderDel(scope.$index, scope.row)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--    分页组件-->
    <el-pagination
      background
      :page-size="pageInfo.pageSize"
      :page-sizes="[5, 10, 20, 30]"
      :current-page="pageInfo.pageNum"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
  </div>
</template>


<script>
import axios from "axios";

export default {
  name: "UserMoneyView",
  data() {
    return {
      total: 0,
      pageInfo: {
        pageNum: 1,
        pageSize: 5,
        bookName:"",
        author:"",
      },
      book: [],
      options:[],
      category:"",

      bookshelf:{
          id:0,
          path:"",
          bookName:"",
          author:"",
          category:"",
          //查询到的订单的价格
          money:0,
          username:"",
          //查询到的user用户的剩余余额
          myMoney:0.0,
          //userId
          userId:0,
      },
      user:{
        id:'',
        username:'',
      },
    }

  },
  // 前置执行方法在执行之前    beforeCreate()   axios.post(get)("http://localhost:8080/queryAll").then(...)
  created() { // 这是可以获取data中的数据后  再执行下面的axios
    this.getData()
  },
  methods: {
    sta(row) {
      if (row.vip == 1) {
        return '普通书籍'
      } else {
        return 'Vip书籍'
      }
    },
    // 修改书籍
    // handleEdit(index, row) {
    //   alert(row.id)
    //   // 使用路由的push方法  跳转到修改书籍页面
    //   this.$router.push({ name: 'BookEditView', query: { id: row.id }})
    // },


    // 修改每页条数
    handleSizeChange(val) {
      this.pageInfo.pageSize = val
      this.getData()
    },
    // 修改当前页数
    handleCurrentChange(val) {
      this.pageInfo.pageNum = val
      this.getData()
    },
    // 获取数据
    getData() {
      //获取所有的书籍类型
      axios({
        url: '/api/classification/queryAllCategory',
        method: 'post',
        headers:{"Authorization":"Bearer "+this.$store.state.user.token}
      }).then(res => {
        // console.log(res.data.data)
        this.options=res.data.data
      })

      //首先根据用户姓名查询用户的个人信息
      var formData = new FormData();
      formData.append("username", this.$store.state.user.name)
      axios({
        //根据用户名查询用户的个人信息
        url: '/api/user/queryUser',
        method: 'post',
        data: formData,
        headers:{"Authorization":"Bearer "+this.$store.state.user.token}
      }).then(res => {
        this.user=res.data.data
      //  用户的id  res.data.data.id
      //根据用户的id 查询用户和订单关联表u_o中的 orderids
      //查询所有的数据
        var formData = new FormData();
        for (const arr in this.pageInfo) {
          formData.append(arr, this.pageInfo[arr])
        }
        formData.append("category", this.category)
        formData.append("userId", res.data.data.id)
        axios({
          //根据用户id查询用户的订单信息  和订单的集合
          url: '/api/order/queryAllOrder',
          method: 'post',
          data: formData,
          headers:{"Authorization":"Bearer "+this.$store.state.user.token}
        }).then(res => {
          res.data.data.list.forEach(item=>{
            if(item.state==1){
              item.state="待支付"
            }else if(item.state==2){
              item.state="支付成功"
            }
          })

          this.book=res.data.data.list
          this.total = res.data.data.total
        })
        // this.money=res.data.data.list


      })



    },
    // 查询
    onSubmit() {
      // alert(this.pageInfo.username)
      this.pageInfo.pageNum = 1 // 点击查询时  页码重置为1

      this.getData()
    },

    //付费添加到书架
    handleAdd(index,row) {

      if (confirm('本次购买需要'+row.money+'元，是否确定要购买吗?')) {
        // alert(this.$store.state.user.name)    '/api/order/queryMoney',
        //判断余额是否充足
        if(this.user.money>=row.money){
          this.bookshelf.username=this.$store.state.user.name
          this.bookshelf.myMoney=this.bookshelf.myMoney+this.user.money
          for (const key in row) {
            this.bookshelf[key]=row[key]
          }
          this.bookshelf.userId=this.user.id
          //修改订单状态 (支付完成与否)
          axios({
            url: '/api/order/pay/' + row.id,
            method: 'post',
            data: this.bookshelf,
            headers: {"Authorization": "Bearer " + this.$store.state.user.token}
          }).then(res => {
            if (res.data.code == 1) {
              this.$message({
                type:'success',
                message: '支付成功,已经放进书架中了'
              })
              this.addOrderMessages(row)
              this.getData()
            }else {
              this.$message({
                type: 'error',
                message: '支付失败,请稍后重新尝试'
              })
            }
          })
        }else{
          if (confirm('余额不足,购买需要'+row.money+'元，是否去充值?')) {
            //跳转到充值界面
            this.$router.push({ name: 'RechargeView' })

          }
        }


      //     // console.log(res.data)
      //       //金钱 res.data.data
      //       // var data1 = new FormData();
      //       // data1.append("username", this.$store.state.user.name)
      //       // data1.append("money", res.data.data)
      //
      //       // var formData = new FormData();
      //       // formData.append("id", row.id)
      //       // data1.append("bookshelf", this.bookshelf)
      //       // formData.append("category", row.category)
      //
      //
      //
      //
      //       // // console.log(this.bookshelf)
      //       // axios({
      //       //   url: '/api/order/pay/' + row.id,
      //       //   method: 'post',
      //       //   data: this.bookshelf,
      //       //   headers: {"Authorization": "Bearer " + this.$store.state.user.token}
      //       // }).then(res => {
      //       //   if (res.data.code == 1) {
      //       //     this.$message({
      //       //       message: '支付成功,已经放进书架中了',
      //       //       type: 'success'
      //       //     })
      //       //     //添加到书架中   id(bookid)
      //       //     // BookShelf bookShelf, Integer id, Integer intestate,Integer uid
      //       //     //根据订单id查询bookid
      //       //
      //       //
      //       //     // //在用户支付成功后  给用户添加一条消息
      //       //     //   var formData2 = new FormData();
      //       //     //   formData2.append("uids", this.user.id)
      //       //     //   //添加消息对象到消息数组中
      //       //     //   formData2.append("messages", "支付成功,书籍详情请移步到我的书架")
      //       //     //   axios({
      //       //     //     url: '/api/messages/addMessages',
      //       //     //     method: 'post',
      //       //     //     data: formData,
      //       //     //     headers:{"Authorization":"Bearer "+this.$store.state.user.token}
      //       //     //   })
      //       //
      //       //
      //       //
      //       //
      //       //
      //       //     // 更新本页数据
      //       //     this.getData()
      //       //   } else {
      //       //     this.$message.error('支付失败')
      //       //   }
      //       // })
      //       // 更新本页数据
      //       // this.getData()
      //
      //
      }
      },

    // 删除医生信息
    orderDel(index, row) {
      // get post
      if (confirm('确定要删除吗')) {
        axios({
          url: '/api/order/delOrder/' + row.id,
          method: 'post',
          headers:{"Authorization":"Bearer "+this.$store.state.user.token}
        }).then(res => {
          if (res.data.code == 1) {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            // 更新本页数据
            this.getData()
          } else {
            this.$message.error('删除失败')
          }
        })
      }
    },
    // this.$router.push({name:})


    //添加书籍的接口
    addShelf(row){
      var formData = new FormData();
      formData.append("id", row.id)
      formData.append("userId", this.user.id)
      // formData.append("category", row.category)
      for (const key in row) {
        // console.log(key)
        // console.log(row[key])
        formData.append(key, row[key])
      }
      axios({
        url: '/api/bookshelf/add',
        method: 'post',
        data:formData,
        headers:{"Authorization":"Bearer "+this.$store.state.user.token},
      }).then(res => {
        if (res.data.code ==1) {
          this.$message({
            message: '添加成功',
            type: 'success'
          })
          this.getData()
        } else {
          this.$message({
            message: '添加失败',
            type: 'error'
          })
        }
      })
    },

    //添加添加支付成功消息的接口
    addOrderMessages(row){
      var formData = new FormData();
      formData.append("messages", row.bookName+"支付成功,已经添加到书架中了,详情请移步到书架")
      formData.append("ids", this.user.id)
      axios({
        url: '/api/messages/addMessages',
        method: 'post',
        data: formData,
        headers:{"Authorization":"Bearer "+this.$store.state.user.token}
      })
    },














    },






}
</script>

<style scoped>

</style>
